<template>
  <div class="container1">
    <div class="top">
      <div class="top_item" v-for="(item, index) in topBox" :key="index">
        <div class="flex_left">
          <div>
            <img
              :src="require(`../../assets/dashboard/top/${item.icon}.png`)"
            />
          </div>
          <div>
            {{ item.name }}
            <div class="value">
              {{ item.value || 0
              }}<span class="gray">{{
                item.name == "公司利润率" ? "%" : "万元"
              }}</span>
            </div>
          </div>
        </div>
        <div class="flex_right">
          <span class="gray">环比{{ item.huanbi < 0 ? "减少" : "增加" }}</span>
          <img
            :src="
              require(`../../assets/dashboard/top/${
                item.huanbi < 0 ? 'down_arrow' : 'top_arrow'
              }.png`)
            "
          />
          <span :class="item.huanbi < 0 ? 'green' : 'red'"
            >{{ item.huanbi }}%</span
          >
        </div>
      </div>
    </div>
    <div class="middle">
      <div class="view_left">
        <div class="title">经营所概览</div>
        <div class="flexBox">
          <div class="flexItem" v-for="(item, index) in manageBox" :key="index">
            <div class="top1">
              <div class="dep">{{ item.department.name }}</div>
              <div class="name">所长 ：{{ item.department.leaderLabel }}</div>
            </div>
            <div class="middle1">
              <div style="text-align: center">
                <div class="digital">{{projectNum(item.projectCnt) }}</div>
                项目数量
              </div>
              <div style="text-align: center">
                <div>
                  <span class="digital">{{ item.revenue / 10000 || 0 }}</span>
                  万元
                </div>
                所产值
              </div>
              <div style="text-align: center">
                <div>
                  <span class="digital">{{ item.profit }}</span
                  >万元
                </div>
                利润
              </div>
            </div>
            <div style="display: flex; justify-content: center">
              <div v-if="message" class="message">
                距离目标，还差{{
                  item.department.monGoal / 10000 - item.revenue / 10000 || 0
                }}万元
              </div>
              <div v-else class="message2">
                超过目标{{
                  item.revenue / 10000 - item.department.monGoal / 10000 || 0
                }}万元
              </div>
            </div>
            <div class="borderStyle"></div>
            <div class="middle1">
              <div style="text-align: center">
                <div class="digital">{{ item.missionAmountDTO.missionCnt?item.missionAmountDTO.missionCnt["0"]:'0' }}</div>
                任务数
              </div>
              <div style="text-align: center">
                <div class="digital">{{ item.missionAmountDTO.missionCnt?item.missionAmountDTO.missionCnt["2"]:'0' }}</div>
                完成数
              </div>
              <div style="text-align: center">
                <div>
                  <span class="digital">
                      {{item.missionAmountDTO.missionCnt?(item.missionAmountDTO.missionCnt["2"] /item.missionAmountDTO.missionCnt["0"]) * 100:'0'}}
                  </span
                  >%
                </div>
                完成率
              </div>
            </div>
            <div class="borderStyle"></div>
            <div class="detail" @click="showDialog(item)">所详情</div>
          </div>
        </div>
      </div>
      <div class="rank_right">
        <div class="title">经营所利润排行</div>
        <div class="pro">
          <div
            :class="['profitF', currentNav == 1 ? 'profit_active' : 'profit']"
            @click="profit"
          >
            利润
          </div>
          <div
            :class="['profitF', currentNav == 2 ? 'profit_active' : 'profit']"
            @click="profitMargin"
          >
            利润率
          </div>
        </div>
        <div ref="chart1" class="chartsBox"></div>
      </div>
    </div>
    <div class="middle">
      <div class="view_left">
        <div class="title">生产所概览</div>
        <div class="flexBox">
          <div
            class="flexItem"
            v-for="(item, index) in productionBox"
            :key="index"
          >
            <div class="top1">
              <div class="dep">{{ item.department.name }}</div>
              <div class="name">所长 ：{{ item.department.leaderLabel }}</div>
            </div>
            <div class="middle1">
              <div style="text-align: center">
                <div class="digital">{{ projectNum(item.projectCnt) }}</div>
                项目数量
              </div>
              <div style="text-align: center">
                <div>
                  <span class="digital">{{ item.revenue / 10000 || 0 }}</span>
                  万元
                </div>
                所产值
              </div>
              <div style="text-align: center">
                <div>
                  <span class="digital">{{ item.profit }}</span
                  >万元
                </div>
                利润
              </div>
            </div>
            <div style="display: flex; justify-content: center">
              <div v-if="message" class="message">
                距离目标，还差{{
                  item.department.monGoal / 10000 - item.revenue / 10000 || 0
                }}万元
              </div>
              <div v-else class="message2">
                超过目标{{
                  item.revenue / 10000 - item.department.monGoal / 10000 || 0
                }}万元
              </div>
            </div>
            <div class="borderStyle"></div>
            <div class="middle1">
              <div style="text-align: center">
                <div class="digital">{{ item.missionAmountDTO.missionCnt?item.missionAmountDTO.missionCnt["0"]:'0' }}</div>
                任务数
              </div>
              <div style="text-align: center">
                <div class="digital">{{ item.missionAmountDTO.missionCnt?item.missionAmountDTO.missionCnt["2"]:'0' }}</div>
                完成数
              </div>
              <div style="text-align: center">
                <div>
                  <span class="digital">{{item.missionAmountDTO.missionCnt?(item.missionAmountDTO.missionCnt["2"] /item.missionAmountDTO.missionCnt["0"]) * 100:'0'}}</span
                  >%
                </div>
                完成率
              </div>
            </div>
            <div class="borderStyle"></div>
            <div class="detail" @click="showDialog(item)">所详情</div>
          </div>
        </div>
      </div>
      <div class="rank_right">
        <div class="title">生产所利润排行</div>
        <div class="pro">
          <div
            :class="['profitF', currentNav2 == 1 ? 'profit_active' : 'profit']"
            @click="profit2"
          >
            利润
          </div>
          <div
            :class="['profitF', currentNav2 == 2 ? 'profit_active' : 'profit']"
            @click="profitMargin2"
          >
            利润率
          </div>
        </div>
        <div ref="chart2" class="chartsBox"></div>
      </div>
    </div>
    <BjDialog
      ref="bjDialog"
      :data="suoData"
      :memberName="member"
      :timeEd="timeEd"
      :timeSt="timeSt"
    ></BjDialog>
  </div>
</template>
<script>
import { setFontSize } from "@/utils/tool";
import BjDialog from "@/components/bj-dialog/index.vue";
import * as echarts from "echarts";
import { suoStatistics} from "@/api/dashborad";
import { getLastMonthDate } from "@/utils/time";
export default {
  components: {
    BjDialog,
  },
  name: "dashboard",
  props: ["timeEd", "timeSt", "id"],
  watch: {
    timeEd: function (val) {
      console.log(val); // 接收父组件的值
      this.getSuoInformation(this.timeSt, val, this.id);
    },
    timeSt: function (val) {
      console.log(val); // 接收父组件的值
      this.getSuoInformation(val, this.timeEd, this.id);
    },
    id: function (val) {
      console.log(val); // 接收父组件的值
      this.getSuoInformation(this.timeSt, this.timeEd, val);
    },
  },
  data() {
    return {
      message: true,
      message2: true,
      suoData: {},
      member: [],
      currentNav: 1,
      currentNav2: 1,
      chartProfitM: [],
      chartProfitP: [],
      chartProfitMarginM: [],
      chartProfitMarginP: [],
      topBox: [
        {
          name: "公司总支出",
          icon: "top1",
          value: 0,
          huanbi: 0,
        },
        {
          name: "公司总收入",
          icon: "top2",
          value: 0,
          huanbi: 0,
        },
        {
          name: "公司利润",
          icon: "top3",
          value: 0,
          huanbi: 0,
        },
        {
          name: "公司利润率",
          icon: "top4",
          value: 123.43,
          huanbi: 0,
        },
      ],
      manageBox: [],
      productionBox: [],
    };
  },
  mounted() {
    this.initLineChart("chart1", this.chartProfitM);
    this.initLineChart("chart2", this.chartProfitP);
  },
  created() {
    this.getSuoInformation(this.timeSt, this.timeSt, this.id);
  },
  methods: {
    // 项目数量
    projectNum(item) {
      return item.reduce((all, el) => all + el["count(project_schedule)"],0)
    },
    // 获取指定所任务数量
    // async suoTaskCount(id) {
    //   try {
    //     const res = await suoTaskCount(id, {
    //       monEd: +this.timeEd.split("-")[1],
    //       monSt: +this.timeSt.split("-")[1],
    //       yearEd: +this.timeEd.split("-")[0],
    //       yearSt: +this.timeSt.split("-")[0],
    //     });
    //     if (res && res.code == 200) {
    //     this.taskAll = res.result.missionCnt["0"];
    //       this.taskWc = res.result.missionCnt["2"];
    //     }
    //
    //   } catch (error) {
    //     this.taskAll = 0;
    //     this.taskWc = 0;
    //     // console.log(error);
    //   }
    // },
    async changeTime(star, end) {
      console.log(star, end, "---------10.7");
      const res2 = await suoStatistics({
        createTimeEd: star,
        createTimeSt: getLastMonthDate(star),
        mainObjId: this.id,
      });
      let zc = res2.result.reduce((all, el) => all + el.expenses, 0) / 10000;
      let sr = res2.result.reduce((all, el) => all + el.revenue, 0) / 10000;
      let lr = res2.result.reduce((all, el) => all + el.profit, 0) / 10000;
      let lrl = ((lr / zc) * 100).toFixed(2);
      this.topBox[0].huanbi = (
        ((this.topBox[0].value - zc) / zc) * 100 || 0
      ).toFixed(2);
      this.topBox[1].huanbi = (
        ((this.topBox[1].value - sr) / sr) * 100 || 0
      ).toFixed(2);
      this.topBox[2].huanbi = (
        ((this.topBox[2].value - lr) / lr) * 100 || 0
      ).toFixed(2);
      this.topBox[3].huanbi = (
        ((this.topBox[3].value - lrl) / lrl) * 100 || 0
      ).toFixed(2);
      // console.log(res2,23333333)
      // console.log(this.getLastMonthDate(star),333333)
    },
    // 通过时间和公司查询所信息
    async getSuoInformation(st, ed, id) {
      // debugger
      const res = await suoStatistics({
        createTimeEd: ed,
        createTimeSt: st,
        mainObjId: id,
      });
      console.log(res, id);
      this.topBox[0].value =
        res.result.reduce((all, el) => all + el.expenses, 0) / 10000;
      this.topBox[1].value =
        res.result.reduce((all, el) => all + el.revenue, 0) / 10000;
      this.topBox[2].value =
        res.result.reduce((all, el) => all + el.profit, 0) / 10000;
      this.topBox[3].value = (
        (this.topBox[2].value / this.topBox[0].value) * 100 || 0
      ).toFixed(2);
      this.manageBox = res.result.filter((item) => item.department.type == 2);
      this.productionBox = res.result.filter(
        (item) => item.department.type == 3
      );
      this.manageBox.forEach((x) => {
        let mes = (x.department.monGoal - x.revenue) / 10000;
        if (mes < 0) return (this.message = false);
        this.message = true;
      });
      this.productionBox.forEach((x) => {
        let mes = (x.department.monGoal - x.revenue) / 10000;
        if (mes < 0) return (this.message2 = false);
        this.message2 = true;
      });
      this.chartProfitM = this.manageBox.map((item) => {
        return {
          name: item.department.name,
          value: item.profit,
        };
      });
      this.chartProfitP = this.productionBox.map((item) => {
        return {
          name: item.department.name,
          value: item.profit,
        };
      });
      this.chartProfitMarginM = this.manageBox.map((item) => {
        return {
          name: item.department.name,
          value: (item.profitMargin || 0).toFixed(2),
        };
      });
      this.chartProfitMarginP = this.productionBox.map((item) => {
        return {
          name: item.department.name,
          value: (item.profitMargin || 0).toFixed(2),
        };
      });
      this.initLineChart("chart1", this.chartProfitM);
      this.initLineChart("chart2", this.chartProfitP);
      // const res1=await suoTaskCount()
    },
    profit() {
      this.currentNav = 1;
      this.initLineChart("chart1", this.chartProfitM);
    },
    profitMargin() {
      this.currentNav = 2;
      this.initLineChart("chart1", this.chartProfitMarginM);
    },
    profit2() {
      this.currentNav2 = 1;
      this.initLineChart("chart2", this.chartProfitP);
    },
    profitMargin2() {
      this.currentNav2 = 2;
      this.initLineChart("chart2", this.chartProfitMarginP);
    },
    // 打开项目详情弹框
    showDialog(data) {
      console.log("打开项目详情弹框", data);
      this.suoData = data;
      this.member = JSON.parse(data.department.member);
      this.$refs.bjDialog.showDialog = true;
      // console.log(this.suoData)
    },
    initLineChart(refName, data) {
      let yAxisData = data.map((item) => item.name);
      // let salvProMax =[];//背景按最大值
      let chart = this.$echarts.init(this.$refs[refName]);
      let option = {
        title: {
          text: "单位(万元)",
          textStyle: {
            color: ["#ffffff"],
            fontStyle: ["微软雅黑"],
            fontSize: setFontSize(10),
          },
          left: setFontSize(1),
          top: setFontSize(1),
        },
        tooltip: {
          trigger: "axis",
          backgroundColor: "rgba(0,0,0,0.5)",
          axisPointer: {
            type: "none",
          },
          formatter: function (params) {
            var relVal = params[0].name;
            let marker = `<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:#ffed00;"></span>`;
            return (
              relVal +
              "<br/>" +
              marker +
              params[0].seriesName +
              "&nbsp" +
              "&nbsp" +
              "&nbsp" +
              "&nbsp" +
              params[0].data +
              "万元"
            );
          },
        },
        grid: {
          top: setFontSize(20),
          left: setFontSize(60),
          right: setFontSize(20),
          bottom: setFontSize(20),
        },
        xAxis: [
          {
            gridIndex: 0,
            axisTick: {
              lineStyle: {
                color: ["#d9e1e7"],
              },
            },
            axisLabel: {
              show: true,
              textStyle: {
                fontSize: setFontSize(12),
                color: "#ffffff",
              },
            },
            splitLine: {
              lineStyle: {
                color: ["rgba(90, 150, 95, 0.2)"],
              },
            },
            axisLine: {
              show: false,
            },
          },
        ],
        yAxis: [
          {
            gridIndex: 0,
            interval: 0,
            data: yAxisData.reverse(),
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: true,
              textStyle: {
                fontSize: setFontSize(10),
                color: "#ffffff",
              },
            },
            splitLine: {
              show: false,
            },
            axisLine: {
              show: false,
            },
          },
        ],
        series: [
          {
            name: "利润",
            type: "bar",
            barGap: "-90%",
            barCategoryGap: "-90%",
            showBackground: true,
            barWidth: "40%",
            backgroundStyle: {
              color: "rgba(46, 117, 89,0.1)",
            },
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  1,
                  0,
                  [
                    {
                      offset: 0,
                      color: "rgba(9, 200, 137, 0)",
                    },
                    {
                      offset: 0.8,
                      color: "rgba(248, 246, 97, 1)",
                    },
                  ],
                  false
                ),
              },
            },
            label: {
              normal: {
                show: true,
                position: "right",
                textStyle: {
                  color: "#F8F661",
                },
              },
            },
            data: data.map((item) => item.value),
          },
        ],
      };
      chart.setOption(option);
      window.addEventListener("resize", function () {
        chart.resize();
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.amap-layers canvas {
  position: relative;
}
.container1 {
  //position: relative;
  height: 960px;
  width: 100%;
  pointer-events: all;
  padding: 16px 20px;
  .top {
    display: flex;
    justify-content: space-between;
    margin-bottom: 16px;
    .top_item {
      width: 458px;
      height: 113px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 20px;
      background: url("../../assets/dashboard/top/bgc.png") no-repeat 0 0;
      background-size: 100% 100%;
      .gray {
        font-size: 14px;
        font-family: "微软雅黑";
        color: #a9bbc7;
      }
      .flex_left {
        display: flex;
        align-items: center;
        font-weight: bold;
        img {
          width: 58px;
          height: 65px;
          vertical-align: middle;
        }
        .value {
          font-size: 44px;
          font-family: DS-Digital-Bold, DS-Digital;
          font-weight: bold;
          color: #f6c838;
        }
      }
      .flex_right {
        img {
          vertical-align: middle;
          margin: 0 3px;
          width: 13px;
          height: 13px;
        }
        .green {
          font-size: 28px;
          font-family: DS-Digital;
          font-weight: bold;
          color: #00dda9;
        }
        .red {
          font-size: 28px;
          font-family: DS-Digital;
          font-weight: bold;
          color: #dd0000;
        }
      }
    }
  }
  .middle {
    display: flex;
    justify-content: space-between;
    margin-bottom: 16px;
    .title {
      position: absolute;
      top: 18px;
      left: 50%;
      transform: translateX(-50%);
      font-size: 28px;
      font-family: "庞门正道标题体";
      color: #ffffff;
      letter-spacing: 2px;
    }
    .view_left {
      position: relative;
      width: 1406px;
      height: 408px;
      background: url("../../assets/dashboard/middle/bgL.png") no-repeat 0 0;
      background-size: 100% 100%;
      padding-left: 20px;

      .flexBox {
        display: flex;
        margin-top: 70px;
        flex-wrap: wrap;
        height: 325px;
        overflow-y: scroll;
        .flexItem {
          position: relative;
          width: 330px;
          height: 310px;
          margin-right: 15px;
          margin-bottom: 15px;
          background: rgba(0, 44, 82, 0.8);
          border: 1px solid #09ccff;
          .top1 {
            display: flex;
            justify-content: space-between;
            background: linear-gradient(180deg, #00b0ff 0%, #005489 100%);
            border: 1px solid rgba(9, 204, 255, 0.6);
            padding: 8px 16px;
            margin-bottom: 11px;
            font-size: 14px;
            color: #ffffff;
            .dep {
              font-size: 16px;
              font-family: "微软雅黑";
              font-weight: bold;
              color: #ffffff;
            }
          }
          .middle1 {
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 14px;
            //margin-bottom: 18px;
            margin: 16px;
            .digital {
              font-size: 28px;
              font-family: DS-Digital-Bold, DS-Digital;
              font-weight: bold;
              color: #f6c838;
            }
          }
          .borderStyle {
            margin: 0 16px;
            width: 298px;
            height: 1px;
            border: 1px solid;
            border-image: linear-gradient(
                270deg,
                rgba(9, 210, 255, 0),
                rgba(15, 222, 255, 0.7),
                rgba(21, 235, 255, 0)
              )
              1 1;
          }
          .message {
            padding: 3px 10px;
            background: rgba(255, 0, 0, 0.4);
            text-align: center;
            border-radius: 2px;
            font-size: 14px;
            margin-bottom: 20px;
          }
          .message2 {
            padding: 3px 10px;
            background: rgba(0, 216, 255, 0.4);
            text-align: center;
            border-radius: 2px;
            font-size: 14px;
            margin-bottom: 20px;
          }
          .detail {
            position: absolute;
            left: 50%;
            bottom: 18px;
            margin: 0 0 0 -32px;
            width: 64px;
            height: 24px;
            font-size: 14px;
            text-align: center;
            background: linear-gradient(180deg, #27daff 0%, #005678 100%);
            border-radius: 2px;
            border: 1px solid #00e7ff;
          }
        }
      }
    }
    .rank_right {
      position: relative;
      width: 458px;
      height: 408px;
      background: url("../../assets/dashboard/middle/bgR.png") no-repeat 0 0;
      background-size: 100% 100%;
      padding: 119px 34px 0 34px;
      .pro {
        display: flex;
        position: absolute;
        top: 78px;
        left: 50%;
        transform: translateX(-50%);
        font-size: 14px;
        color: #58f3ff;
        .profitF {
          width: 64px;
          height: 28px;
          text-align: center;
          line-height: 28px;
          background: rgba(0, 37, 60, 0.8);
          border-radius: 2px;
          border: 1px solid #09d2ff;
          margin-right: 16px;
        }
        .profit_active {
          box-shadow: inset 0px 0px 20px 0px #05b3ff;
        }
        .profit {
          box-shadow: inset 0px 0px 20px 0px #05a1ff;
          opacity: 0.7;
        }
      }
      .chartsBox {
        width: 100%;
        height: 261px;
      }
    }
  }
}
::v-deep .el-select-dropdown {
  background-color: #162d49 !important;
  //color: red;
}
::v-deep .el-select-dropdown__item.hover,
::v-deep .el-select-dropdown__item:hover {
  color: #1084e3;
  background-color: #01dde7;
}

::v-deep .el-popper .popper__arrow,
.el-popper .popper__arrow::after {
  display: none;
}

::v-deep .el-select-dropdown {
  background: rgba(6, 39, 64, 0.8) !important;
  border: 1px solid #1adbfe;
  box-shadow: inset 0 0 2px 0 #00ceff;
  border-radius: 3px;
}

::v-deep .el-select-dropdown__item {
  color: #e0eaff;
}

.el-select-dropdown__list {
}
::v-deep .el-popper {
  background: rgba(6, 39, 64, 0.8) !important;
  border: 1px solid #1adbfe;
  box-shadow: inset 0 0 2px 0 #00ceff;
  border-radius: 3px;
}
</style>
